//
//  GYMineShopAddressVC.m
//  GYDemo
//
//  Created by mac on 2018/6/20.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineShopAddressVC.h"
#import "Head.h"

@interface GYMineShopAddressVC ()
@property (nonatomic,strong) GYMineShopAddressViewModel *viewModel;
@property (nonatomic,strong) UIButton *saveBtn;
@end

@implementation GYMineShopAddressVC
#pragma mark - getter and setter
-(GYMineShopAddressViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [GYMineShopAddressViewModel shareInterface];
    }
    return _viewModel;
}
-(UIButton *)saveBtn
{
    if (!_saveBtn) {
        _saveBtn = [UIButton new];
        _saveBtn.x = 0;
        _saveBtn.width = self.view.width;
        _saveBtn.height = WFCGFloatY(50);
        _saveBtn.y = self.view.height - _saveBtn.height;
        _saveBtn.titleLabel.font = SYSTEMFONT(15);
        [_saveBtn setTitle:@"添加收货地址" forState:UIControlStateNormal];
        [_saveBtn setBackgroundColor:Main_Color_Red forState:UIControlStateNormal];
        [_saveBtn setTitleColor:Main_Color_White forState:UIControlStateNormal];
        [_saveBtn addTarget:self action:@selector(saveBtnTap:) forControlEvents:UIControlEventTouchUpInside];
        _saveBtn.layer.borderColor = Main_Color_White.CGColor;
        _saveBtn.layer.borderWidth = 4;
    }
    return _saveBtn;
}
#pragma mark - event
-(void)saveBtnTap:(id)sender
{
    GYMineShopAddressEditVC *edit = [GYMineShopAddressEditVC new];
    edit.viewModel.listViewModel = self.viewModel;
    [self.navigationController pushViewController:edit animated:YES];
}
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = @"收货地址管理";
    self.extendedLayoutIncludesOpaqueBars = YES;
    
    if (@available(iOS 11.0, *)) {
        self.baseTable.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        // Fallback on earlier versions
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    self.baseTable.contentInset = UIEdgeInsetsMake([self topShelterHeight], 0, [self bottomShelterHeight], 0);
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.height = self.saveBtn.y;
    self.baseTable.scrollIndicatorInsets = self.baseTable.contentInset;
    UIView *foot = [UIView new];
    foot.height = self.saveBtn.height;
    [self.baseTable setTableFooterView:foot];
    
    [self.view addSubview:self.saveBtn];
    [self viewModelBand];
    @WeakSelf;
    self.baseTable.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [selfp requestData];
    }];
    [self requestData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.viewModel;
    observation.keyPath = @"datas";
    observation.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation];
}
-(void)requestData
{
    [self.viewModel requestDatas];
}
-(void)reloadView
{
    [self.baseTable reloadData];
}

#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
#pragma mark - table
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.datas.count;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return WFCGFloatY(45);
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYMineShopAddressCell *cell = (GYMineShopAddressCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    CGFloat height = [cell finalHeight];
    return height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"AddressVCCell";
    GYMineShopAddressModel *model = self.viewModel.datas[indexPath.row];
    GYMineShopAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[GYMineShopAddressCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    @WeakSelf;
    [cell.editBTN handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        GYMineShopAddressEditVC *edit = [GYMineShopAddressEditVC new];
        edit.viewModel.listViewModel = selfp.viewModel;
        edit.viewModel.indexPath = indexPath;
        [selfp.navigationController pushViewController:edit animated:YES];
    }];
    [cell.deleteBTN handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您确定删除吗？" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alert showAlertViewWithCompleteBlock:^(NSInteger buttonIndex) {
            if (1 == buttonIndex) {
                [selfp.viewModel requestDeleteIndex:indexPath.row];
            }
        }];
    }];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_ischoose) {
        
        GYMineShopAddressModel *address = self.viewModel.datas[indexPath.row];
        self.viewModel.defaultAddress = address;
        
        self.handler(self.viewModel.datas[indexPath.row]);
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
